﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using WebConfigModifier.Samples;

namespace WebConfigModifier.Tests
{
    public class UpdateWebConfigTests
    {
        [TestClass]
        public class WhenUpdatingAnAttributeWithTheSameValue
        {
            string _webConfigFilename;

            Stack<string> _args;
            string _value;

            string _contentsBefore;
            string _contentsAfterwards;

            [TestInitialize]
            public void Setup()
            {
                _contentsBefore = Resources.Mvc2_Vanilla_Web;

                _webConfigFilename = "Mvc2.Vanilla.Web.Config";
                File.WriteAllText(_webConfigFilename, _contentsBefore);

                _args = new Stack<string>(new[] {
                        _webConfigFilename,
                        "/attribute",
                        "/configuration/connectionStrings/add[@name='ApplicationServices']",
                        "providerName" }
                    .Reverse()  // stack initialiser reverses the order once already
                    );

                _value = "System.Data.SqlClient";

                new WebConfigUpdater().HandleRequest(_args, _value);

                _contentsAfterwards = File.ReadAllText(_webConfigFilename);
            }

            [TestMethod]
            public void NoExceptionsShouldBeThrown()
            {
            }
        }

        [TestClass]
        public class WhenUpdatingAnAttributeWithADifferentValue
        {
            string _webConfigFilename;

            Stack<string> _args;
            string _value;

            string _contentsBefore;
            string _contentsAfterwards;

            [TestInitialize]
            public void Setup()
            {
                _contentsBefore = Resources.Mvc2_Vanilla_Web;

                _webConfigFilename = "Mvc2.Vanilla.Web.Config";
                File.WriteAllText(_webConfigFilename, _contentsBefore);

                _args = new Stack<string>(new[] {
                        _webConfigFilename,
                        "/attribute",
                        "/configuration/connectionStrings/add[@name='ApplicationServices']",
                        "providerName" }
                    .Reverse()  // stack initialiser reverses the order once already
                    );

                _value = "REPLACED_PROVIDER_NAME";

                new WebConfigUpdater().HandleRequest(_args, _value);

                _contentsAfterwards = File.ReadAllText(_webConfigFilename);
            }

            [TestMethod]
            public void TheWebConfigFileShouldContainTheNewValue()
            {
                Assert.IsTrue(_contentsAfterwards.Contains(_value));
            }
        }

        [TestClass]
        public class WhenUpdatingAnElementWithADifferentValue
        {
            string _webConfigFilename;

            Stack<string> _args;
            string _value;

            string _contentsBefore;
            string _contentsAfterwards;
            string _expectedSubstring;

            [TestInitialize]
            public void Setup()
            {
                _contentsBefore = Resources.Mvc2_Vanilla_Web;

                _webConfigFilename = "Mvc2.Vanilla.Web.Config";
                File.WriteAllText(_webConfigFilename, _contentsBefore);

                _args = new Stack<string>(new[] {
                        _webConfigFilename,
                        "/element",
                        "/configuration/connectionStrings",
                        }
                    .Reverse()  // stack initialiser reverses the order once already
                    );

                _value = Resources.connectionStrings;

                new WebConfigUpdater().HandleRequest(_args, _value);

                _contentsAfterwards = File.ReadAllText(_webConfigFilename);

                _expectedSubstring = "This connection string section has been replaced in its entirety.";
            }

            [TestMethod]
            public void TheWebConfigFileShouldContainTheNewValue()
            {
                Assert.IsTrue(_contentsAfterwards.Contains(_expectedSubstring));
            }
        }

    }
}
